import QQMapWX from '../lib/qqmap-wx-jssdk.min';

// key: 'CALBZ-CGVWK-7S5JW-AUV63-23PBE-D5F7L'
const qqmapsdk = new QQMapWX({
    key: '3BIBZ-JLBCD-4U24G-H6VRU-F4PKQ-RUBIV'
});

// 获取坐标转地址后的详细信息
const getReverseGeocoder = function () {
    return new Promise((resolve, reject) => {
        getLatLng().then(res => {
            const { latitude, longitude } = res;
            qqmapsdk.reverseGeocoder({
                //位置坐标，默认获取当前位置，非必须参数
                location: { latitude, longitude },
                success: function (res) {
                    resolve(res.result);
                }
            });
        }, error => {
            reject(error);
        });
    });
}

// 获取当前位置详细信息
const getAddress = function () {
    return new Promise((resolve, reject) => {
        getReverseGeocoder().then(res => {
            resolve(res);
        }, error=> {
            reject(error);
        });
    });
}


// 获取用户定位信息（经纬度）
const getLatLng = function () {
    return new Promise((resolve, reject) => {
        wx.getLocation({
            type: 'wgs84',
            success(res) {
                const latitude = res.latitude;
                const longitude = res.longitude;
                resolve({ latitude, longitude })
            },
            fail(error) {
                reject(error);
            },
        })
    });
}

// 搜索周边
const searchPeriphery = function (keyword, latitude, longitude, page) {
    return new Promise((resolve, reject) => {
        qqmapsdk.search({
            keyword,  //搜索关键词
            location: { latitude, longitude },  //设置周边搜索中心点
            page_index: page, // 第x页，默认第1页
            success: function (res) { //搜索成功后的回调
                resolve(res);
            },
            fail: function (res) {
                console.log(res);
                reject(res);
            }
        });
    });
}

// 关键字输入
const keywordInput = function (keyword, page, cityName) {
    return new Promise((resolve, reject) => {
        qqmapsdk.getSuggestion({
            //获取输入框值并设置keyword参数
            keyword, //用户输入的关键词，可设置固定值,如keyword:'KFC'
            page_index: page, // 第x页，默认第1页
            region: cityName, //设置城市名，限制关键词所示的地域范围，非必填参数
            success: function (res) {//搜索成功后的回调
                // console.log(res);
                resolve(res);
            },
            fail: function (error) {
                console.error(error);
                reject(error);
            },
        });
    })
}

// 获取城市列表
const getCityList = function () {
    return new Promise((resolve, reject) => {
        qqmapsdk.getCityList({
            success: function (res) {//成功后的回调
                // console.log(res);
                // console.log('省份数据：', res.result[0]); //打印省份数据
                // console.log('城市数据：', res.result[1]); //打印城市数据
                // console.log('区县数据：', res.result[2]); //打印区县数据
                resolve(res.result[1]);
            },
            fail: function (error) {
                console.error(error);
                reject(error);
            }
        });
    });
}

// 路线规划
const routePlan = function (from, to) {
    return new Promise((resolve, reject) => {
        qqmapsdk.direction({
            mode: 'driving', // 默认 driving
            from,
            to,
            success: function (res) {
                resolve(res);
            },
            fail: function (error) {
                reject(error);
            }
        });
    })
}

// polyline 坐标解压
const decompression = function (points) {
    let coors = points;
    if (Array.isArray(points) && points.length > 0) {
        for (let i = 2; i < coors.length; i++) {
            coors[i] = coors[i - 2] + coors[i] / 1000000;
        }
    }
    return coors;
}

// 获取缩放级别
const getScale = function (distance) {
    let scale;
    if (distance && distance > 0) {
        if (distance < 25) { // 距离小于25米
            scale = 18;
        } else if (distance >= 25 && distance < 50) { // 小于50大于等于25
            scale = 17;
        } else if (distance >= 50 && distance < 100) {
            scale = 17;
        } else if (distance >= 100 && distance < 200) {
            scale = 16;
        } else if (distance >= 200 && distance < 500) {
            scale = 15;
        } else if (distance >= 500 && distance < 1000) {
            scale = 14;
        } else if (distance >= 1000 && distance < 2000) {
            scale = 12;
        } else if (distance >= 2000 && distance < 5000) {
            scale = 13;
        } else if (distance >= 5000 && distance < 10000) {
            scale = 12;
        } else if (distance >= 10000 && distance < 20000) {
            scale = 11;
        } else if (distance >= 20000 && distance < 30000) {
            scale = 10;
        } else if (distance >= 30000 && distance < 50000) {
            scale = 9;
        } else if (distance >= 50000 && distance < 100000) {
            scale = 8;
        } else if (distance >= 100000 && distance < 200000) {
            scale = 7;
        } else {
            scale = 6;
        }
    }
    return scale;
}


module.exports = {
    getAddress,
    searchPeriphery,
    keywordInput,
    getCityList,
	routePlan,
	decompression,
	getScale
}